package 第四周;

/*
 * @author  YxinMiracle
 * @date  2022-04-28 17:25
 * @Gitee: https://gitee.com/yxinmiracle
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 电话号码的字母组合 {
    public Map<Character,String> map = new HashMap<>();
    public String digits;
    public List<String> ans = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits.equals("")){ // 特殊判断
            return ans;
        }
        this.digits = digits;
        map.put('2',"abc");
        map.put('3',"def");
        map.put('4',"ghi");
        map.put('5',"jkl");
        map.put('6',"mno");
        map.put('7',"pqrs");
        map.put('8',"tuv");
        map.put('9',"wxyz");
        dfs(0,"");
        return ans;
    }

    /**
     * @param i 当前递归的层数
     * @param newDigits 在递归过程中创建的字符串
     */
    private void dfs(int i, String newDigits) {
        if (i == digits.length()){
            ans.add(newDigits);
            return;
        }

        for (char keyboard : map.get(digits.toCharArray()[i]).toCharArray()) {
            dfs(i+1,newDigits+keyboard);
        }
    }

    public static void main(String[] args) {
        System.out.println(new 电话号码的字母组合().letterCombinations("23"));
    }

}
